// PROGRAMA : CTLPC004
// BIBLIOTECA : PRODETR
// SISTEMA : INFORMACOES DE SEGURANCA PUBLICA - SISP
// CLIENTE : SEJUSP - SEC. DA JUSTICA E SEGURANCA PUBLICA
// INSTITUTO DE IDENTIFICACAO
// PROGRAMADOR : EDU/FM DATA: 09/06/94
// OBJETIVO : CONSULTA DOC. CANCELADOS P/INTERV. DE DATA. 'CDD'
// ATUALIZACOES DATA
// PROGRAMA : CTLPC004 04/11/1997
// BIBLIOTECA : SISP
// PROGRAMADOR :
// OBJETIVO : ADAPTACAO PARA O SHOPPING DO CIDADAO (P200)
// : ALTERACAO DE TELA 30/09/1999
// : ALTERACAO DE DATA N6 PARA N8 06/10/1999
// OBS : ESTE PROGRAMA FOI REESTRUTURADO POR SOLON MIRANDA
package br.com.vti.sisp_sicrim.antigo.controller.programa;

import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.interceptor.download.InputStreamDownload;
import br.com.vti.sisp_sicrim.antigo.controller.relatorios.Ctlpc004Report;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Ctlpc004A;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Ctltc001;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Ctltc003;
import br.com.vti.sisp_sicrim.antigo.model.SicControle;
import br.com.vti.sisp_sicrim.antigo.model.Sisldat8;
import br.com.vti.sisp_sicrim.antigo.model.Sisll001;
import br.com.vti.sisp_sicrim.antigo.model.Tablele;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.Sisgsisp;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Ctlpc004 OBJETIVO : CONSULTA DOC. CANCELADOS P/INTERV. DE DATA. 'CDD' *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/ctlpc004")
public class Ctlpc004 {
	// Views utilizadas
	// vwDvecad = new;
	SicControle vwDvecon = new SicControle();
	SicControle vwDvecon2 = new SicControle();
	// Telas/relatorios utilizados
	Ctlpc004A ctlpc004A;
	// Ctlpc004B ctlpc004B;
	Ctltc001 ctltc001;
	Ctltc003 ctltc003;
	Ctlpc004Report report;
	// Includes utilizados
	Sisldat8 sisldat8 = new Sisldat8();
	Sisll001 sisll001 = new Sisll001();
	Tablele tablele = new Tablele();
	// Areas globais / session
	Sisgsisp sisgsisp;
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();

	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	Ctlpc004 ctlpc004Anterior;
	String faseproc = "0";
	InputStreamDownload relatoriopdf;

	private int i = 0;
	// private String nomfun = "";
	private long valpago = 0;

	private Ctlpc004A preMapas() {
		int ind;
		ctlpc004A = new Ctlpc004A(true, sessao);
		// ctlpc004B = new Ctlpc004B(true, sessao);
		ctltc001 = new Ctltc001(true, sessao);
		ctltc003 = new Ctltc003(true, sessao);
		for (ind = 1; ind <= 2; ind++) {
			ctlpc004A.setConimp(ind, "");
		}
		for (ind = 1; ind <= 2; ind++) {
			ctltc003.setDoc0(ind, "");
			ctltc003.setEmi0(ind, 0);
			ctltc003.setMot0(ind, "");
			ctltc003.setMat0(ind, 0);
			ctltc003.getNum0()[i] = 0;
		}
		ctltc003.setDataCorrente(UtilBean.obtemDataCorrente());
		ctltc003.setDataCorrente(UtilBean.inverteDataCorrente());
		// Par�grafo: inicio()
		return ctlpc004A;
	}

	public Ctlpc004(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public InputStreamDownload executar(Ctlpc004A ctlpc004A, Ctltc001 ctltc001, Ctltc003 ctltc003) {
		try {
			main(ctlpc004A, ctltc001, ctltc003);
			if (relatoriopdf != null && !result.used()) {
				return relatoriopdf;
			}
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroSiscerro(e, sessao, this.getClass().getSimpleName());
		}

		return null;
	}

	private void main(Ctlpc004A pctlpc004A, Ctltc001 pctltc001, Ctltc003 pctltc003) throws Exception {
		if (pctltc003 == null && pctltc001 == null && pctlpc004A == null) {
			primeiraVez = true;
			// ctltc003 = new Ctltc003 (primeiraVez);
			ctlpc004A = preMapas();
			ctlpc004A.setNavegar("../ctlpc004/executar");
			result.include("ctlpc004A", ctlpc004A);
			sessao.setAtributo("CTLPC004", this);
			ctlpc004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		} else {
			restaurarEstados(pctlpc004A, pctltc001, pctltc003);
		}

		if (pctlpc004A != null && pctlpc004A.getFuncao() != null && pctlpc004A.getFuncao().equals("3")) {
			Idcpm001 idcpm001 = new Idcpm001(result, sessao);
			idcpm001.executar(null);

			return;
		}

		if (pctltc001 != null) {

			if (pctltc001.getConf().equals("N") || pctltc001.getFuncao().equals("3")) {
				ctltc003.setMensagem("CONSULTA INTERROMPIDA");
				ctltc003.setNavegar("../ctlpc004/executar");
				result.include("ctltc003", ctltc003);
				sessao.setAtributo("CTLPC004", this);
				ctltc003.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (pctltc001.getConf().equals("S")) {
				if (saida(ctltc003, ctltc001)) {
					// ctltc001.enviarMensagem("opc", "PROCESSAR PARA");
					ctltc001.setNavegar("../ctlpc004/executar");
					sessao.setAtributo("CTLPC002", this);
					result.include("ctltc001", ctltc001);
					ctltc001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				} else {
					ctltc003.setMensagem("CONSULTA INTERROMPIDA");
					ctltc003.setNavegar("../ctlpc004/executar");
					result.include("ctltc003", ctltc003);
					sessao.setAtributo("CTLPC004", this);
					ctltc003.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());

				}

			}

			return;
		}

		if (pctltc003 != null && pctltc003.getFuncao() != null && pctltc003.getFuncao().equals("3")) {

			ctlpc004A = preMapas();
			ctlpc004A.setNavegar("../ctlpc004/executar");
			result.include("ctlpc004A", ctlpc004A);
			sessao.setAtributo("CTLPC004", this);
			ctlpc004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		}

		// DEFINE PRINTER(FM = 1)
		ctltc003.setConf("S");

		ctlpc004A.receberTela(false, result);
		if (!UtilBean.funcao.equals(UtilBean.ctt_processa)) {
			ctlpc004A.enviarMensagem(null, "");
			return;
		}
		if (!ctlpc004A.getOperacao().equals("CDD")) {
			testar();
		}
		if (!(ctlpc004A.getTidoem() == 44 || ctlpc004A.getTidoem() == 87 || ctlpc004A.getTidoem() == 90 || ctlpc004A.getTidoem() == 92)) {

			/*
			 * if (!ctlpc004B.getEnviada()) { ctlpc004B.setEnviada(true); sessao.setAtributo("CTLPC004", this); result.include("ctlpc004B", ctlpc004B); ctlpc004B.enviarTela(false, result,
			 * getClass().getSimpleName().toUpperCase()); return; }
			 */
			/*
			 * if (pctlpc004B != null) { ctlpc004B.receberTela(false, result); }
			 */
			// CTLPC004
			if (!(ctlpc004A.getTidoem() == 44 || ctlpc004A.getTidoem() == 87 || ctlpc004A.getTidoem() == 90 || ctlpc004A.getTidoem() == 92)) {
				ctlpc004A.enviarMensagem(null, "TIPO DOCUMENTO INVALIDO");
				sessao.setAtributo("CTLPC004", this);
				result.include("ctlpc004A", ctlpc004A);
				ctlpc004A.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			// continue;
		}
		switch (sessao.getSisgsisp().getVar()) {
		case 07: {
			if (!(ctlpc004A.getTidoem() == 44)) {
				ctlpc004A.enviarMensagem("tidoem", "TIPO DOCUMENTO INVALIDO PARA O MODULO");
				ctlpc004A.setNavegar("../ctlpc004/executar");
				result.include("ctlpc004A", ctlpc004A);
				sessao.setAtributo("CTLPC004", this);
				ctlpc004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			break;
		}
		case 13: {
			if (!(ctlpc004A.getTidoem() == 87 || ctlpc004A.getTidoem() == 90 || ctlpc004A.getTidoem() == 92)) {
				ctlpc004A.enviarMensagem("tidoem", "TIPO DOCUMENTO INVALIDO PARA O MODULO");
				ctlpc004A.setNavegar("../ctlpc004/executar");
				result.include("ctlpc004A", ctlpc004A);
				sessao.setAtributo("CTLPC004", this);
				ctlpc004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			break;
		}
		default: {
			ctlpc004A.enviarMensagem(null, "NAO EXISTE CODIGO CANCELAMENTO P/O MODULO");
			ctlpc004A.setNavegar("../ctlpc004/executar");
			result.include("ctlpc004A", ctlpc004A);
			sessao.setAtributo("CTLPC004", this);
			ctlpc004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		}

		if (!UtilBean.validarMascara(ctlpc004A.getDatini(), "yyyyMMdd")) {
			ctlpc004A.enviarMensagem("operacao", "");
			ctlpc004A.setNavegar("../ctlpc004/executar");
			result.include("ctlpc004A", ctlpc004A);
			sessao.setAtributo("CTLPC004", this);
			ctlpc004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		if (!UtilBean.validarMascara(ctlpc004A.getDatfim(), "yyyyMMdd")) {
			ctlpc004A.enviarMensagem("adt", "");
			ctlpc004A.setNavegar("../ctlpc004/executar");
			result.include("ctlpc004A", ctlpc004A);
			sessao.setAtributo("CTLPC004", this);
			ctlpc004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		if (ctlpc004A.getDatini() > ctlpc004A.getDatfim()) {
			ctlpc004A.enviarMensagem("operacao", "");
			ctlpc004A.setNavegar("../ctlpc004/executar");
			result.include("ctlpc004A", ctlpc004A);
			sessao.setAtributo("CTLPC004", this);
			ctlpc004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		if (UtilBean.validaSelecao(ctlpc004A.getConimp())) {
			ctlpc004A.enviarMensagem("tidoem", "");
			ctlpc004A.setNavegar("../ctlpc004/executar");
			result.include("ctlpc004A", ctlpc004A);
			sessao.setAtributo("CTLPC004", this);
			ctlpc004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		}

		// INVERTE DATA
		if (!ctlpc004A.getConimp(2).trim().equals("")) {
			relatoriopdf = imprime();
		} else {

			if (ctltc003.getConf().equals("N")) {

				ctlpc004A.setMensagemSaida("CONSULTA INTERROMPIDA OK");
				ctlpc004A.setNavegar("../ctlpc004/executar");
				result.include("ctlpc004A", ctlpc004A);
				sessao.setAtributo("CTLPC004", this);
				ctlpc004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;

			}

			if (ctltc003.getOpc() > 0) {
				if (saida(ctltc003, ctltc001)) {
					// ctltc001.enviarMensagem("opc", "PROCESSAR PARA");
					ctltc001.setNavegar("../ctlpc004/executar");
					sessao.setAtributo("CTLPC002", this);
					result.include("ctltc001", ctltc001);
					ctltc001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				} else {
					ctltc003.setMensagem("CONSULTA INTERROMPIDA");
					ctltc003.setNavegar("../ctlpc004/executar");
					result.include("ctltc003", ctltc003);
					sessao.setAtributo("CTLPC004", this);
					ctltc003.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());

				}
				return;
			}

			ctltc003.setDtini(UtilBean.inverteData(ctlpc004A.getDatini()));
			ctltc003.setDtfim(UtilBean.inverteData(ctlpc004A.getDatfim()));

			ctltc003.setEmi0(new int[15]);
			ctltc003.setEmi0(new int[15]);
			ctltc003.setDoc0(new String[15]);

			// PESQUISA TABELA
			ctltc003.setDoc0(new String[15]);
			ctltc003.setMat0(new int[15]);
			ctltc003.setMot0(new String[15]);
			ctltc003.setNum0(new long[15]);
			boolean lista = false;
			while (vwDvecon.pesquisaDataEmissaoFaixa(ctlpc004A.getDatini(), ctlpc004A.getDatfim())) {

				switch (sessao.getSisgsisp().getVar()) {
				case 07: {
					if (!(vwDvecon.getTipoDocumento() == 44)) {
						continue;
					}
					break;
				}
				case 13: {
					if (!(vwDvecon.getTipoDocumento() == 87 || vwDvecon.getTipoDocumento() == 90 || vwDvecon.getTipoDocumento() == 92)) {
						continue;
					}
					break;
				}
				default: {
					continue;
				}
				}
				/*
				 * if (vwDvecon.fimDados()) { if (i > 0 && ctltc001.getOperacao().equals("CDD")) { // if (!saida(pctltc003, pctltc001)) return; } }
				 */
				/*
				 * if (ctlpc004A.getTidoem() > 0) { if (!(vwDvecon.getTipoDocumento() == ctlpc004A.getTidoem())) { continue; } }
				 */
				i += 1;
				lista = true;
				ctltc003.setEmi0(i, vwDvecon.getDataEmissao());
				ctltc003.setEmi0(i, (int) funcoes.sissd002(ctltc003.getEmi0(i)));
				ctltc003.setDoc0(i, "");
				tablele.pesq = UtilBean.moveSubstring(tablele.pesq, 0, 3, "N", 19);
				tablele.pesq = UtilBean.moveSubstring(tablele.pesq, 3, 12, "N", vwDvecon.getTipoDocumento());
				tablele.chalfa = funcoes.sisst002(19, vwDvecon.getTipoDocumento(), tablele.chalfa);
				// PESQUISA TABELA
				ctltc003.setDoc0(i, tablele.chalfa);
				ctltc003.setMat0(i, vwDvecon.getMatriculaFunc());
				ctltc003.setMot0(i, vwDvecon.getEnderecoRequerente());
				ctltc003.getNum0()[i] = vwDvecon.getNumeroDocumento();
				// ctlpc004A.setMensagemSaida("CONSULTA EFETUADA OK");

				if (i == 14 || vwDvecon.fimDados()) {
					/*
					 * if (!saida(ctltc003, ctltc001)) { ctltc003.setMensagem("PROCESSAR PARA CONTINUAR OU N�O(N) PARA SAIR"); break; }
					 */

					if (vwDvecon.fimDados()) {
						ctltc003.setMensagem("FIM DA CONSULTA N�O(N) SAIR OU SELECIONE O REGISTRO");
						break;
					} else {
						ctltc003.setMensagem("PROCESSAR PARA CONTINUAR OU N�O(N) PARA SAIR");
					}

					break;
				}

			}
			if (vwDvecon.registrosProcessados() == 0) {
				ctlpc004A.enviarMensagem(null, "NAO ENCONTROU REGISTROS COM AS INFORMA��ES");
				ctlpc004A.setNavegar("../ctlpc004/executar");
				result.include("ctlpc004A", ctlpc004A);
				sessao.setAtributo("CTLPC004", this);
				ctlpc004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}

			if (lista) {
				ctltc003.setEnviada(true);
				sessao.setAtributo("CTLPC004", this);
				result.include("ctltc003", ctltc003);
				ctltc003.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			} else {
				ctltc003.setMensagem("FIM DA CONSULTA");
			}

		}

		if (relatoriopdf == null && !result.used()) {
			retorno();
		}

	}

	private void retorno() {
		sessao.setAtributo("CTLPC004", this);
		result.include("ctltc003", ctltc003);
		ctltc003.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
	}

	private boolean saida(Ctltc003 ctltc003, Ctltc001 ctltc001) throws Exception {

		// ctltc003.setMensagemSaida("CONSULTA EFETUADA OK");

		if (ctltc003 != null) {
			ctltc003.receberTela(false, result);
		}
		if (!ctltc003.getOperacao().equals("CDD")) {
			testar();
		}
		boolean achou = false;
		if (ctltc003.getOpc() > 0) {

			i = 1;
			for (int i2 = 1; i2 <= ctltc003.getNum0().length; i2++) {
				if (ctltc003.getNum0()[i] != 0) {
					i++;
				}

			}
			if (ctltc003.getOpc() > i) {
				ctltc003.enviarMensagem("opc", "NUMERO SELECIONADO INVALIDO");
				sessao.setAtributo("CTLPC002", this);
				result.include("ctltc003", ctltc003);
				ctltc003.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return false;
			}

			if (ctltc003.getOpc() > i) {
				ctltc003.enviarMensagem("opc", "NUMERO SELECIONADO INVALIDO");
				return false;
			}

			// do {
			// 206

			while (vwDvecon2.leituraNumeroDocumentoMaiorig(ctltc003.getNum0()[ctltc003.getOpc()])) {
				// READADA
				switch (sessao.getSisgsisp().getVar()) {
				case 07: {
					if (!(vwDvecon2.getTipoDocumento() == 44)) {
						continue;
					}
					break;
				}
				case 13: {
					if (!(vwDvecon2.getTipoDocumento() == 87 || vwDvecon2.getTipoDocumento() == 90 || vwDvecon2.getTipoDocumento() == 92)) {
						continue;
					}
					break;
				}
				default: {
					continue;
				}
				}
				if (ctlcc001(ctltc001)) {
					achou = true;
					break;
				}

				// TELA/CONSULTA DOCUMENTOS EMITIDOS
				if (!ctltc001.getChave().trim().equals("")) {
					continue;
				}
			}

			/*
			 * if (ctltc003.getConf().equals("N") && ctltc001.getOperacao().equals("CDD")) { ctltc003.setConf("S");
			 * 
			 * if (!ctltc003.getEnviada()) { sessao.setAtributo("CTLPC004", this); result.include("ctltc003", ctltc003); ctltc003.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			 * return false; } if (ctltc003 != null) { ctltc003.receberTela(false, result); } } // TELA RELACAO DOS CANCELADOS if (ctltc003.getConf().equals("N")) {
			 * 
			 * ctlpc004A.setMensagemSaida("CONSULTA INTERROMPIDA OK"); break; } return true; } while (!(ctltc003.getOpc() == 0));
			 */
		}
		i = 0;
		/*
		 * for (int i1 = 1; i1 <= 14; i1++) { ctltc003.setDoc0(i1, ""); ctltc003.setEmi0(i1, 0); ctltc003.setMot0(i1, ""); ctltc003.setMat0(i1, 0); ctltc003.setNum0(i1, 0); }
		 */
		if (!ctltc001.getOperacao().equals("CDD")) {
			testar();
		}

		if (achou) {
			if (vwDvecon2.fimDados()) {
				ctltc001.setMensagem("FIM DA CONSULTA N�O(N) SAIR");
			} else {
				ctltc001.setMensagem("PROCESSAR PARA CONTINUAR OU N�O(N) PARA SAIR");
			}
		}

		return achou;
	}

	private InputStreamDownload imprime() throws Exception {

		report = new Ctlpc004Report();
		report.setDtini(ctlpc004A.getDatini());
		report.setDtini((int) funcoes.sissd002(report.getDtini()));
		// INVERTE DATA
		report.setDtfim(ctlpc004A.getDatfim());
		report.setDtfim((int) funcoes.sissd002(report.getDtfim()));

		report.setPageSize(60);
		report.setLineSize(133);
		report.setDataCorrente(UtilBean.obtemDataCorrente());
		report.gravaRelatorio(1);
		while (vwDvecon.pesquisaDataEmissaoFaixa(ctlpc004A.getDatini(), ctlpc004A.getDatfim())) {

			if (ctlpc004A.getTidoem() > 0) {
				if (!(vwDvecon.getTipoDocumento() == ctlpc004A.getTidoem())) {
					continue;
				}
			}
			switch (sessao.getSisgsisp().getVar()) {
			case 07: {
				if (!(vwDvecon.getTipoDocumento() == 44 || vwDvecon.getTipoDocumento() == 78 || vwDvecon.getTipoDocumento() == 86)) {
					continue;
				}
				break;
			}
			case 13: {
				if (!(vwDvecon.getTipoDocumento() == 87 || vwDvecon.getTipoDocumento() == 90 || vwDvecon.getTipoDocumento() == 92)) {
					continue;
				}
				break;
			}
			default: {
				continue;
			}
			}
			// public void setDiacntendere(String diacntendere) throws
			// ParseException {
			// public void setDincntndemid(long dincntndemid) throws
			// ParseException {
			// public void setDiacntchassi(String diacntchassi) throws
			// ParseException {
			report.setDincntmatfun(vwDvecon.getMatriculaFunc());
			report.setDiacntendere(vwDvecon.getEnderecoRequerente());
			report.setDiacntchassi(vwDvecon.getNumeroChassi());
			report.setDincntndemid(vwDvecon.getNumeroDocumento());

			report.setTipod(String.valueOf(vwDvecon.getTipoDocumento()));
			report.setDatemi(vwDvecon.getDataEmissao());
			report.setDatemi((int) funcoes.sissd002(report.getDatemi()));
			// INVERTE DATAS
			tablele.pesq = UtilBean.moveSubstring(tablele.pesq, 0, 3, "N", 19);
			tablele.pesq = UtilBean.moveSubstring(tablele.pesq, 3, 12, "N", vwDvecon.getTipoDocumento());
			report.setTipod("");
			tablele.chalfa = funcoes.sisst002(19, vwDvecon.getTipoDocumento(), tablele.chalfa);
			// PESQUISA TABELA
			// report = new Ctlpc004Report();
			report.setTipod(tablele.chalfa);
			// RELATORIO FM PS=60 LS=133 SG=OFF

			// RELATORIO
			report.gravaRelatorio(2);
			if (report.getPageLine() == report.getPageSize() - 1) {
				report.gravaRelatorio(1);
			}
			// RELATORIO
		}

		if (vwDvecon.totalRegistros() == 0) {
			ctlpc004A.enviarMensagem(null, "NAO ENCONTROU REGISTROS COM AS INFORMA��ES");
			ctlpc004A.setNavegar("../ctlpc004/executar");
			result.include("ctlpc004A", ctlpc004A);
			sessao.setAtributo("CTLPC004", this);
			ctlpc004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		} else {

		}

		return report.imprimir();
	}

	private boolean ctlcc001(Ctltc001 pctltc001) throws Exception {
		// INCLUDE : CTLCC001(COPYCODE)
		// BIBLIOTECA : SISP
		// SISTEMA : INFORMACOES DE SEGURANCA PUBLICA
		// - SISP
		// CLIENTE : SEJUSP - SEC. DA JUSTICA E
		// SEGURANCA PUBLICA
		// INSTITUTO DE IDENTIFICACAO, DETRAN E SEFAZ
		// PROGRAMADOR : EDU/FM DATA: 27/05/92
		// OBJETIVO : CONSULTA DADOS NO ARQUIVO DE
		// CONTROLE(COPYCODE)
		// ATUALIZACOES DATA
		// INCLUDE : CTLCC001 04/11/1997
		// BIBLIOTECA : SISP
		// PROGRAMADOR :
		// OBJETIVO : ADAPTACAO PARA O SHOPPING DO
		// CIDADAO (P200)
		// : ALTERACAO DA ROTINA SISSD001 (DATA N6)
		// PARA
		// SISSD002 (DATA N8)
		// 30/09/1999
		// OBS : ESTE COPYCODE FOI REESTRUTURADO POR
		// SOLON MIRANDA

		ctltc001.setDatemi(vwDvecon2.getDataEmissao());
		ctltc001.setDatemi(funcoes.sissd002(ctltc001.getDatemi()));
		// INVERTE DATA
		valpago = vwDvecon2.getValorPago();
		tablele.resp = funcoes.sisst001(18, vwDvecon2.getMatriculaFunc(), tablele.resp);
		// PESQUISA TABELA
		ctltc001.setNomfun(tablele.resp);
		tablele.chalfa = funcoes.sisst002(19, vwDvecon2.getTipoDocumento(), tablele.chalfa);
		// PESQUISA TABELA
		ctltc001.setTipdoc(tablele.chalfa);

		/*
		 * if (!ctltc001.getEnviada()) { sessao.setAtributo("CTLPC004", this); result.include("ctltc001", ctltc001); ctltc001.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
		 * return false; }
		 */
		/*
		 * if (pctltc001 != null) { ctltc001.receberTela(false, result); }
		 */
		// TELA CONSULTA DOCUMENTOS EMITIDOS
		if (!(ctltc001.getOperacao().equals("CEC") || ctltc001.getOperacao().equals("CEN") || ctltc001.getOperacao().equals("CED") || ctltc001.getOperacao().equals("CDC") || ctltc001.getOperacao().equals("CDD") || ctltc001.getOperacao().equals("ELD"))) {
			testar();
		}
		/*
		 * if (ctltc003.getConf().equals("N")) { return false; }
		 */
		if (!ctltc001.getChave().trim().equals("")) {

			ctlpc004A.setMensagemSaida("CONSULTA INTERROMPIDA OK");
			ctlpc004A.setNavegar("../ctlpc004/executar");
			result.include("ctlpc004A", ctlpc004A);
			sessao.setAtributo("CTLPC004", this);
			ctlpc004A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return false;
		}
		ctltc001.setTipdoc("");
		return true;
	}

	private void testar() {
		funcoes.sissm001(result, sessao);
	}

	private void restaurarEstados(Ctlpc004A pctlpc004A, Ctltc001 pctltc001, Ctltc003 pctltc003) {
		this.ctlpc004Anterior = (Ctlpc004) sessao.getAtributo("CTLPC004");
		if (pctlpc004A != null) {
			ctlpc004A = pctlpc004A;
		} else {
			ctlpc004A = this.ctlpc004Anterior.ctlpc004A;
		}
		ctlpc004A.setSessao(sessao);
		// if (pctlpc004B != null) {
		// ctlpc004B = pctlpc004B;
		// } else {
		// ctlpc004B = this.ctlpc004Anterior.ctlpc004B;
		// }
		// ctlpc004B.setSessao(sessao);
		if (pctltc001 != null) {
			ctltc001 = pctltc001;
			vwDvecon = ctlpc004Anterior.vwDvecon;
			vwDvecon2 = ctlpc004Anterior.vwDvecon2;

		} else {
			ctltc001 = this.ctlpc004Anterior.ctltc001;
		}
		ctltc001.setSessao(sessao);
		if (pctltc003 != null) {
			ctltc003 = pctltc003;
			vwDvecon = ctlpc004Anterior.vwDvecon;
		} else {
			ctltc003 = this.ctlpc004Anterior.ctltc003;
		}
		ctltc003.setSessao(sessao);
		this.faseproc = this.ctlpc004Anterior.faseproc;
		ctltc003.setEnviada(ctlpc004Anterior.ctltc003.getEnviada());
	}
}
